<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Log;
use Session;
use Illuminate\Support\Facades\DB;

class WechatController extends Controller
{
    //

    public function serve()
    {
        Log::info('request arrived.'); # 注意：Log 为 Laravel 组件，所以它记的日志去 Laravel 日志看，而不是 EasyWeChat 日志

        $wechat = app('wechat');
        $wechat->server->setMessageHandler(function($message){
            return "欢迎关注 overtrue！";
        });

        Log::info('return response.');

        return $wechat->server->serve();
    }

    public function test(){
        var_dump(147);
    }

    //用户授权
    public function userAuth(){
        $app = app('wechat');
        $oauth = $app->oauth;
        //获取OAuth授权结果用户信息
        $user = $oauth->user();
        session(['openid'=> $user->id, 'wechat_user'=>$user]);
        $userInfo = DB::table('user')->where(['openid' => $user->id])->first();
        if(!$userInfo) {
            $insertData = [
                'openid' => $user->getId(),
                'nickname' => $user->getNickname(),
                'avatar' => $user->getAvatar(),
                'token' => $user->getToken(),
                'created_at' => date('Y-m-d H:i:s')
            ];
            $inserId = DB::table('user')->insert($insertData);
            if ($inserId <= 0){
                echo 'error';die;
            }
        }
        $targetUrl = empty($_SESSION['target_url']) ? '/index' : $_SESSION['target_url'];
        return redirect('/index');
    }

    //问卷调查页面
    public function index(){
        $openid = session('openid');
        $app = app('wechat');
        $js = $app->js;
        // 判断用户是否已经填写
        $userInfo = DB::table('user')->where(['openid' => $openid])->first();
        if ($userInfo && $grade = $userInfo->grade) {
            return view('grade', compact('grade', 'js'));
        }

        return view('index', compact('openid', 'js'));
    }
    //清除cookie
    public function clear(){
        $_SESSION['wechat_user'] = '';
        if ($_SESSION['wechat_user'] == ''){
            echo 'success';
        } else {
            echo 'error';
        }
    }

    //生成二维码
    public function createQrcode()
    {
        $app = app('wechat');
        $qrcode = $app->qrcode;
        $result = $qrcode->temporary('http://www.baidu.com', 6 * 24 * 3600);
        return $result->url;
    }

    //分享
    public function share(){
        $openid = session('openid');
        $data = [
            'openid' => $openid,
            'num' => 1,
            'created_at' => date('Y-m-d H:i:s')
        ];
        $inserId = DB::table('wx_share')->insert($data);
        $returnData = ['status' => 200, 'msg' => '分享成功'];
        return json_encode($returnData);
    }
    //生成结果图片
    public function createResult(Request $request){
        $grade = $request->grade ? $request->grade : 10;
        $openid = session('openid');
        DB::table('user')->where(['openid' => $openid])->update(['grade' => $grade]);
        $url = 'http://wygc.jidiinn.com/images/01.png';
        $js = app('wechat')->js;
        return view('grade', compact('url', 'grade', 'js'));
    }
    //getResult
    public function getResult()
    {
        //人格统计
        $kang = DB::table('user')->whereBetween('grade', [8,12])->count();
        $rose = DB::table('user')->whereBetween('grade', [13, 16])->count();
        $me = DB::table('user')->whereBetween('grade', [17, 20])->count();
        $sun = DB::table('user')->whereBetween('grade', [21, 24])->count();
        //转发量
        $repeat = DB::table('wx_share')->count();
        return view('show', compact('kang', 'rose', 'me', 'sun', 'repeat'));
    }
}
